<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <UploadExcel
          :on-success="handleSuccess"
        />
      </el-card>
    </div>
  </div>
</template>
<script>
import { formatExcelDate } from '@/utils/index'
import { importEmployee } from '@/api/employess'
export default {
  methods: {
    // 读取文件
    async  handleSuccess({ header, results }) {
      console.log(header, results)
      const res = this.transExcel(results)
      await importEmployee(res)
      this.$message.success('导入成功')
      // 返回上一级
      this.$router.back()
      console.log(res)
    },
    transExcel(results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      const res = results.map(item => {
        const zhKeys = Object.keys(item)
        const obj = {}
        zhKeys.forEach(ele => {
          const enKeys = mapInfo[ele]
          if (enKeys === 'timeOfEntry' || enKeys === 'correctionTime') {
            obj[enKeys] = new Date(formatExcelDate(item[ele]))
          } else {
            obj[enKeys] = item[ele]
          }
        })
        return obj
      })
      return res
    }
  }
}
</script>
